package com.socialsoftware.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.socialsoftware.R;
import com.socialsoftware.model.Contact;

import java.util.List;

/**
 * 好友搜索结果适配器
 */
public class FriendSearchResultAdapter extends RecyclerView.Adapter<FriendSearchResultAdapter.ViewHolder> {

    private Context context;
    private List<Contact> contacts;
    private OnItemClickListener listener;
    
    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void onAddButtonClick(Contact contact);
        void onItemClick(Contact contact);
    }
    
    public FriendSearchResultAdapter(Context context, List<Contact> contacts) {
        this.context = context;
        this.contacts = contacts;
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_friend_search_result, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Contact contact = contacts.get(position);
        
        // 设置头像
        // 实际项目中应该使用图片加载库（如Glide或Picasso）从URL加载图片
        // 这里简单使用占位符
        holder.imgAvatar.setImageResource(android.R.drawable.ic_menu_gallery);
        
        // 设置名称
        holder.tvName.setText(contact.getName());
        
        // 设置ID或手机号
        String idOrPhone = "ID: " + contact.getId();
        holder.tvIdOrPhone.setText(idOrPhone);
        
        // 设置地区和备注
        String regionText = "";
        if (contact.getRegion() != null && !contact.getRegion().isEmpty()) {
            regionText = contact.getRegion();
        }
        
        // 如果有备注，添加到地区后面显示
        if (!TextUtils.isEmpty(contact.getRemark())) {
            if (!regionText.isEmpty()) {
                regionText += " | ";
            }
            regionText += contact.getRemark();
        }
        
        if (!regionText.isEmpty()) {
            holder.tvRegion.setText(regionText);
            holder.tvRegion.setVisibility(View.VISIBLE);
        } else {
            holder.tvRegion.setVisibility(View.GONE);
        }
        
        // 根据联系人状态设置按钮
        setupAddButton(holder, contact);
        
        // 设置点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(contact);
                }
            }
        });
    }
    
    /**
     * 根据联系人状态设置添加按钮的状态
     * @param holder ViewHolder
     * @param contact 联系人
     */
    private void setupAddButton(ViewHolder holder, Contact contact) {
        Button btnAdd = holder.btnAdd;
        
        // 特殊类型的联系人（例如已发送请求）
        if (contact.getType() == Contact.TYPE_SPECIAL) {
            btnAdd.setText(R.string.request_sent);
            btnAdd.setEnabled(false);
            btnAdd.setTextColor(context.getResources().getColor(android.R.color.darker_gray));
            return;
        }
        
        // 设置添加按钮点击事件
        btnAdd.setText(R.string.add);
        btnAdd.setEnabled(true);
        btnAdd.setTextColor(context.getResources().getColor(android.R.color.holo_green_dark));
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAddButtonClick(contact);
                }
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return contacts.size();
    }
    
    /**
     * ViewHolder类
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imgAvatar;
        TextView tvName;
        TextView tvIdOrPhone;
        TextView tvRegion;
        Button btnAdd;
        
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imgAvatar = itemView.findViewById(R.id.img_avatar);
            tvName = itemView.findViewById(R.id.tv_name);
            tvIdOrPhone = itemView.findViewById(R.id.tv_id_or_phone);
            tvRegion = itemView.findViewById(R.id.tv_region);
            btnAdd = itemView.findViewById(R.id.btn_add);
        }
    }
} 